package tmall.servlet;

import tmall.bean.Order;
import tmall.dao.OrderDAO;
import tmall.util.Page;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * Servlet for order management.
 * @author Harry Chou
 * @date 2019/3/5
 */
public class OrderServlet extends BaseBackServlet{
    @Override
    public String add(HttpServletRequest request, HttpServletResponse response, Page page) {
        return null;
    }

    @Override
    public String delete(HttpServletRequest request, HttpServletResponse response, Page page) {
        return null;
    }

    @Override
    public String edit(HttpServletRequest request, HttpServletResponse response, Page page) {
        return null;
    }

    @Override
    public String update(HttpServletRequest request, HttpServletResponse response, Page page) {
        return null;
    }

    @Override
    public String list(HttpServletRequest request, HttpServletResponse response, Page page) {
        List<Order> os=orderDAO.list(page.getStart(),page.getCount());
        orderItemDAO.fill(os);
        int total=orderDAO.getTotal();
        page.setTotal(total);

        request.setAttribute("os",os);
        request.setAttribute("page",page);

        return "admin/listOrder.jsp";
    }

    public String delivery(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        Order o = orderDAO.get(id);
        o.setDeliveryDate(new Date());
        o.setStatus(OrderDAO.WAIT_CONFIRM);
        orderDAO.update(o);
        return "@admin_order_list";
    }
}
